package com.linkoog.devtools.http.request.http_request.request_body;

import com.intellij.openapi.project.Project;
import com.intellij.ui.JBCardLayout;
import com.linkoog.devtools.http.request.common.RequestBody;
import com.linkoog.devtools.http.request.common.RequestBodyType;
import com.linkoog.devtools.http.request.common.request_body.NoneRequestBody;
import com.linkoog.devtools.http.request.http_request.request_body.body.FileBodyPanel;
import com.linkoog.devtools.http.request.http_request.request_body.body.MultipartFormPanel;
import com.linkoog.devtools.http.request.http_request.request_body.body.NoneBodyPanel;
import com.linkoog.devtools.http.request.http_request.request_body.body.UrlEncodedFormPanel;
import com.linkoog.devtools.http.request.http_request.request_body.body.text.JSONBodyPanel;
import com.linkoog.devtools.http.request.http_request.request_body.body.text.PlainTextBodyPanel;
import com.linkoog.devtools.http.request.http_request.request_body.body.text.XmlBodyPanel;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.LinkedHashMap;

public class RequestBodyPanel extends JPanel {

    private Project project;
    @NotNull
    private final JBCardLayout cardLayout;


    private RequestBodyType currentBodyType;
    private LinkedHashMap<RequestBodyType, BodyPanel> panels;


    public RequestBodyPanel(Project project) {
        this.project = project;
        this.cardLayout = new JBCardLayout();
        setLayout(cardLayout);

        panels = buildPanels(project);
        panels.forEach((type, bodyPanel) -> add(bodyPanel.getPanel(), type.toString()));
        show(RequestBodyType.NONE);
    }

    public <T extends RequestBody> void resetHttpBody(@NotNull T httpBody){
        final BodyPanel bodyPanel = panels.get(httpBody.bodyType());
        if (bodyPanel != null){
            bodyPanel.resetRequestBody(httpBody);
            show(httpBody.bodyType());
        }
    }

    public RequestBody buildHttpBody() {
        final BodyPanel bodyPanel = panels.get(currentBodyType);
        if (bodyPanel != null){
            return bodyPanel.buildRequestBody();
        }
        return NoneRequestBody.INSTANCE;
    }

    public void show(RequestBodyType requestBodyType){
        currentBodyType = requestBodyType;
        cardLayout.show(this, requestBodyType.toString());
    }


    private LinkedHashMap<RequestBodyType, BodyPanel> buildPanels(Project project){
        LinkedHashMap<RequestBodyType, BodyPanel> panels = new LinkedHashMap<>();
        panels.put(RequestBodyType.NONE, new NoneBodyPanel());
        panels.put(RequestBodyType.JSON, new JSONBodyPanel(project));
        panels.put(RequestBodyType.XML, new XmlBodyPanel(project));
        panels.put(RequestBodyType.PLAIN_TEXT, new PlainTextBodyPanel(project));
        panels.put(RequestBodyType.URL_ENCODED_FORM, new UrlEncodedFormPanel());
        panels.put(RequestBodyType.MULTIPART_FORM, new MultipartFormPanel());
        panels.put(RequestBodyType.FILE, new FileBodyPanel());

        return panels;
    }




}
